package com.blank.shop.dubbo.provider;

import com.blank.shop.dubbo.api.User;
import com.blank.shop.dubbo.api.UserService;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * In-Memory {@link UserService} implementation.
 *
 * @author _blank
 */
@DubboService(protocol = "dubbo")
public class InMemoryUserServiceImpl implements UserService {

    private final Map<Long, User> usersRepository = new HashMap<>();

    @Override
    public boolean save(User user) {
        return usersRepository.put(user.getId(), user) == null;
    }

    @Override
    public boolean remove(Long userId) {
        return usersRepository.remove(userId) != null;
    }

    @Override
    public Collection<User> findAll() {
        return usersRepository.values();
    }

}